package com.mars.service;


import com.mars.dao.UserDAO;
import com.mars.po.LoginForm;
import com.mars.po.User;
import com.mars.utils.DateTime;
import com.mars.utils.Security;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserService  {

    @Autowired
    private UserDAO userDAO;

    public User registe(User user) {
        try {
            user.setPassword(Security.md5Salt(user.getPassword()));
            user.setCreatedAt(DateTime.getTime());
            user.setLoginAt(user.getCreatedAt());
            userDAO.insert(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    public boolean login(LoginForm form) {
        User user = findByEmail(form.getEmail());
        if (user != null && Security.md5Salt(form.getPassword()).equals(user.getPassword())) {
            return true;
        }
        return false;
    }

    public List<User> getAll() {
        List<User> users = null;
        return users;
    }

    public User findByEmail(String email) {
        return userDAO.selectByEmail(email);
    }

    public User findById(int id){
        return userDAO.selectByPrimaryKey(id);
    }


}
